<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.8"/>
<title>AndBase开发框架: com.google.gson.ExclusionStrategy接口 参考</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="ic_launcher.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">AndBase开发框架
   &#160;<span id="projectnumber">1.6</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- 制作者 Doxygen 1.8.8 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'搜索');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>首页</span></a></li>
      <li><a href="pages.html"><span>相关页面</span></a></li>
      <li><a href="namespaces.html"><span>命名空间</span></a></li>
      <li class="current"><a href="annotated.html"><span>类</span></a></li>
      <li><a href="files.html"><span>文件</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="搜索" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="annotated.html"><span>类列表</span></a></li>
      <li><a href="classes.html"><span>类索引</span></a></li>
      <li><a href="hierarchy.html"><span>类继承关系</span></a></li>
      <li><a href="functions.html"><span>类成员</span></a></li>
    </ul>
  </div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>全部</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>类</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>命名空间</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>文件</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>函数</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>变量</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>枚举值</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>页</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="namespacecom.html">com</a></li><li class="navelem"><a class="el" href="namespacecom_1_1google.html">google</a></li><li class="navelem"><a class="el" href="namespacecom_1_1google_1_1gson.html">gson</a></li><li class="navelem"><a class="el" href="interfacecom_1_1google_1_1gson_1_1_exclusion_strategy.html">ExclusionStrategy</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="summary">
<a href="#pub-methods">Public 成员函数</a> &#124;
<a href="interfacecom_1_1google_1_1gson_1_1_exclusion_strategy-members.html">所有成员列表</a>  </div>
  <div class="headertitle">
<div class="title">com.google.gson.ExclusionStrategy接口 参考</div>  </div>
</div><!--header-->
<div class="contents">
<table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a name="pub-methods"></a>
Public 成员函数</h2></td></tr>
<tr class="memitem:a4f32ac5a7cf3e392e4e8dded6f7f5352"><td class="memItemLeft" align="right" valign="top">boolean&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="interfacecom_1_1google_1_1gson_1_1_exclusion_strategy.html#a4f32ac5a7cf3e392e4e8dded6f7f5352">shouldSkipField</a> (<a class="el" href="classcom_1_1google_1_1gson_1_1_field_attributes.html">FieldAttributes</a> f)</td></tr>
<tr class="separator:a4f32ac5a7cf3e392e4e8dded6f7f5352"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:a402d9f2dd7d29d30647b4e93ec278ed7"><td class="memItemLeft" align="right" valign="top">boolean&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="interfacecom_1_1google_1_1gson_1_1_exclusion_strategy.html#a402d9f2dd7d29d30647b4e93ec278ed7">shouldSkipClass</a> (Class&lt;?&gt; clazz)</td></tr>
<tr class="separator:a402d9f2dd7d29d30647b4e93ec278ed7"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">详细描述</h2>
<div class="textblock"><p>A strategy (or policy) definition that is used to decide whether or not a field or top-level class should be serialized or deserialized as part of the JSON output/input. For serialization, if the <a class="el" href="">shouldSkipClass(Class)</a> method returns false then that class or field type will not be part of the JSON output. For deserialization, if <a class="el" href="">shouldSkipClass(Class)</a> returns false, then it will not be set as part of the Java object structure.</p>
<p>The following are a few examples that shows how you can use this exclusion mechanism.</p>
<p><b>Exclude fields and objects based on a particular class type:</b> </p><pre class="code">
private static class SpecificClassExclusionStrategy implements <a class="el" href="interfacecom_1_1google_1_1gson_1_1_exclusion_strategy.html">ExclusionStrategy</a> {
  private final Class&lt;?&gt; excludedThisClass;</pre><pre class="code">  public SpecificClassExclusionStrategy(Class&lt;?&gt; excludedThisClass) {
    this.excludedThisClass = excludedThisClass;
  }</pre><pre class="code">  public boolean shouldSkipClass(Class&lt;?&gt; clazz) {
    return excludedThisClass.equals(clazz);
  }</pre><pre class="code">  public boolean <a class="el" href="interfacecom_1_1google_1_1gson_1_1_exclusion_strategy.html#a4f32ac5a7cf3e392e4e8dded6f7f5352">shouldSkipField(FieldAttributes f)</a> {
    return excludedThisClass.equals(f.getDeclaredClass());
  }
}
</pre><p><b>Excludes fields and objects based on a particular annotation:</b> </p><pre class="code">
public &amp;#64interface FooAnnotation {
  // some implementation here
}</pre><pre class="code">// Excludes any field (or class) that is tagged with an "&amp;#64FooAnnotation"
private static class FooAnnotationExclusionStrategy implements <a class="el" href="interfacecom_1_1google_1_1gson_1_1_exclusion_strategy.html">ExclusionStrategy</a> {
  public boolean shouldSkipClass(Class&lt;?&gt; clazz) {
    return clazz.getAnnotation(FooAnnotation.class) != null;
  }</pre><pre class="code">  public boolean <a class="el" href="interfacecom_1_1google_1_1gson_1_1_exclusion_strategy.html#a4f32ac5a7cf3e392e4e8dded6f7f5352">shouldSkipField(FieldAttributes f)</a> {
    return f.getAnnotation(FooAnnotation.class) != null;
  }
}
</pre><p>Now if you want to configure</p><div class="fragment"><div class="line">Gson </div>
</div><!-- fragment --><p> to use a user defined exclusion strategy, then the</p><div class="fragment"><div class="line">GsonBuilder </div>
</div><!-- fragment --><p> is required. The following is an example of how you can use the </p><div class="fragment"><div class="line">GsonBuilder </div>
</div><!-- fragment --><p> to configure <a class="el" href="classcom_1_1google_1_1gson_1_1_gson.html">Gson</a> to use one of the above sample: </p><pre class="code">
<a class="el" href="interfacecom_1_1google_1_1gson_1_1_exclusion_strategy.html">ExclusionStrategy</a> excludeStrings = new UserDefinedExclusionStrategy(String.class);
<a class="el" href="classcom_1_1google_1_1gson_1_1_gson.html">Gson</a> gson = new GsonBuilder()
    .setExclusionStrategies(excludeStrings)
    .create();
</pre><p>For certain model classes, you may only want to serialize a field, but exclude it for deserialization. To do that, you can write an</p><div class="fragment"><div class="line">ExclusionStrategy </div>
</div><!-- fragment --><p> as per normal; however, you would register it with the <a class="el" href="classcom_1_1google_1_1gson_1_1_gson_builder.html#a39f2ee7f02ef33ae992dbe0d3aacd6c2">GsonBuilder#addDeserializationExclusionStrategy(ExclusionStrategy)</a> method. For example: </p><pre class="code">
<a class="el" href="interfacecom_1_1google_1_1gson_1_1_exclusion_strategy.html">ExclusionStrategy</a> excludeStrings = new UserDefinedExclusionStrategy(String.class);
<a class="el" href="classcom_1_1google_1_1gson_1_1_gson.html">Gson</a> gson = new GsonBuilder()
    .addDeserializationExclusionStrategy(excludeStrings)
    .create();
</pre><dl class="section author"><dt>作者</dt><dd>Inderjeet Singh </dd>
<dd>
Joel Leitch</dd></dl>
<dl class="section see"><dt>参见</dt><dd><a class="el" href="classcom_1_1google_1_1gson_1_1_gson_builder.html#aa1ec5dc5391b4137ef1bda82b883178f">GsonBuilder::setExclusionStrategies</a>(<a class="el" href="interfacecom_1_1google_1_1gson_1_1_exclusion_strategy.html">ExclusionStrategy</a>...) </dd>
<dd>
<a class="el" href="classcom_1_1google_1_1gson_1_1_gson_builder.html#a39f2ee7f02ef33ae992dbe0d3aacd6c2">GsonBuilder::addDeserializationExclusionStrategy(ExclusionStrategy)</a> </dd>
<dd>
<a class="el" href="classcom_1_1google_1_1gson_1_1_gson_builder.html#a253902e8b2d3f9dcdcfe59c913c24f93">GsonBuilder::addSerializationExclusionStrategy(ExclusionStrategy)</a></dd></dl>
<dl class="section since"><dt>自从</dt><dd>1.4 </dd></dl>
</div><h2 class="groupheader">成员函数说明</h2>
<a class="anchor" id="a402d9f2dd7d29d30647b4e93ec278ed7"></a>
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">boolean com.google.gson.ExclusionStrategy.shouldSkipClass </td>
          <td>(</td>
          <td class="paramtype">Class&lt;?&gt;&#160;</td>
          <td class="paramname"><em>clazz</em></td><td>)</td>
          <td></td>
        </tr>
      </table>
</div><div class="memdoc">
<dl class="params"><dt>参数</dt><dd>
  <table class="params">
    <tr><td class="paramname">clazz</td><td>the class object that is under test </td></tr>
  </table>
  </dd>
</dl>
<dl class="section return"><dt>返回</dt><dd>true if the class should be ignored; otherwise false </dd></dl>

</div>
</div>
<a class="anchor" id="a4f32ac5a7cf3e392e4e8dded6f7f5352"></a>
<div class="memitem">
<div class="memproto">
      <table class="memname">
        <tr>
          <td class="memname">boolean com.google.gson.ExclusionStrategy.shouldSkipField </td>
          <td>(</td>
          <td class="paramtype"><a class="el" href="classcom_1_1google_1_1gson_1_1_field_attributes.html">FieldAttributes</a>&#160;</td>
          <td class="paramname"><em>f</em></td><td>)</td>
          <td></td>
        </tr>
      </table>
</div><div class="memdoc">
<dl class="params"><dt>参数</dt><dd>
  <table class="params">
    <tr><td class="paramname">f</td><td>the field object that is under test </td></tr>
  </table>
  </dd>
</dl>
<dl class="section return"><dt>返回</dt><dd>true if the field should be ignored; otherwise false </dd></dl>

</div>
</div>
<hr/>该接口的文档由以下文件生成:<ul>
<li>src/com/google/gson/<a class="el" href="_exclusion_strategy_8java.html">ExclusionStrategy.java</a></li>
</ul>
</div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
生成于 2014年 十一月 6日 星期四 11:22:04 , 为 AndBase开发框架使用  &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.8
</small></address>
</body>
</html>
